package com.greyshark.sanotrive.splash

import android.text.Spannable
import android.text.SpannableString
import android.text.TextPaint
import android.text.method.LinkMovementMethod
import android.text.style.ClickableSpan
import android.text.style.ForegroundColorSpan
import android.view.View
import androidx.fragment.app.FragmentManager
import com.common.base.app.extras.dpDimenPx
import com.common.base.app.fragment.BaseDialogFragment
import com.common.base.utils.AppUtils
import com.greyshark.sanotrive.R
import com.greyshark.sanotrive.databinding.DialogUserPrivacyProtectionBinding

/**
 * 作者：sosou
 * <p>
 * 版本：1.0
 * <p>
 * 创建日期：2022/8/20
 * <p>
 * 描述：用户隐私保护弹框
 * <p>
 * 修订历史：
 */
class UserPrivacyProtectionDialog(private val onAgreeClickListener: View.OnClickListener?) : BaseDialogFragment<DialogUserPrivacyProtectionBinding>(){

    override fun getViewBinding(): DialogUserPrivacyProtectionBinding = DialogUserPrivacyProtectionBinding.inflate(layoutInflater)

    override fun getSelfWidth(): Int = dpDimenPx(R.dimen.dp_282).toInt()

    override fun getSelfHeight(): Int = dpDimenPx(R.dimen.dp_388).toInt()

    override fun getStyle(): Int = STYLE_NORMAL

    override fun initView(view: View) {
        super.initView(view)
        setShowPositionCenter()

        mViewBinding.apply {
            val userProtocol = "《用户协议》"
            val privacy = "《隐私政策》"
            val spannable = SpannableString(getString(R.string.user_privacy_protection_content, getString(R.string.app_name), userProtocol, privacy))

            val start1 = spannable.indexOf(userProtocol)
            val end1 = start1 + userProtocol.length
            spannable.setSpan(ForegroundColorSpan(resources.getColor(R.color.colorPrimaryDark)), start1, end1, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE)
            val clickSpan1 = object: ClickableSpan(){
                override fun updateDrawState(ds: TextPaint) {
                    super.updateDrawState(ds)
                    ds.isUnderlineText = false;// 去掉下划线
                }
                override fun onClick(widget: View) {
//                    LaunchHelper.launchWebActivity(ApiConstant.userProtocol())
                }
            }
            spannable.setSpan(clickSpan1, start1, end1, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE)

            val start2 = spannable.indexOf(privacy)
            val end2 = start2 + privacy.length
            spannable.setSpan(ForegroundColorSpan(resources.getColor(R.color.colorPrimaryDark)), start2, end2, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE)
            val clickSpan2 = object: ClickableSpan(){
                override fun updateDrawState(ds: TextPaint) {
                    super.updateDrawState(ds)
                    ds.isUnderlineText = false;// 去掉下划线
                }
                override fun onClick(widget: View) {
//                    LaunchHelper.launchWebActivity(ApiConstant.privacyProtocol())
                }
            }
            spannable.setSpan(clickSpan2, start2, end2, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE)
            tvContent.movementMethod = LinkMovementMethod.getInstance()
            tvContent.text = spannable

            tvReject.setOnClickListener {
                AppUtils.exitApp()
            }
            tvAgree.setOnClickListener {
                dismiss()
                onAgreeClickListener?.onClick(it)
            }
        }
    }

    override fun show(manager: FragmentManager, tag: String?) {
        super.show(manager, "userPrivacyProtectionDialog")
    }
}